//
//  DDDetailsCommentView.m
//  Dingdong
//
//  Created by 程强 on 16/12/5.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDDetailsCommentView.h"
#import "KSTabButtonView.h"
#import "DDCommentTableCell.h"
#import "DDCommentSet.h"
#import "MJRefresh.h"

@interface DDDetailsCommentView ()<KSTabButtonViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *reviewTable;
@property (strong, nonatomic) LoadMoreBlock moreBlock;
@end

@implementation DDDetailsCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        KSTabButtonView *tabView = [[KSTabButtonView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
        tabView.showIntervalLine = NO;
        tabView.showHoritonzalLine = NO;
        tabView.items = @[@"All Reviews",@"Positive",@"Neutral",@"Negative"];
        tabView.delegate = self;
        tabView.backgroundColor = [UIColor whiteColor];
        [self addSubview:tabView];
        [tabView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.equalTo(self);
            make.top.equalTo(self);
            make.height.equalTo(@44);
        }];
        
        self.reviewTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _reviewTable.delegate = self;
        _reviewTable.dataSource = self;
        _reviewTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_reviewTable];
        [_reviewTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(tabView.mas_bottom).offset(10);
            make.width.equalTo(self);
            make.bottom.equalTo(self);
        }];
        [self.reviewTable registerNib:[UINib nibWithNibName:@"DDCommentTableCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
        self.reviewTable.estimatedRowHeight = 280;
        self.reviewTable.rowHeight = UITableViewAutomaticDimension;
    }
    return self;
}
- (void)ksTabButtonView:(KSTabButtonView *)tabView didSelectIndex:(NSInteger)index
{
    DingDongCommentType type = DingDongCommentTypeAll;
    switch (index) {
        case 0:
            type = DingDongCommentTypeAll;
            break;
        case 1:
            type = DingDongCommentTypePositive;
            break;
        case 2:
            type = DingDongCommentTypeNeutral;
            break;
        case 3:
            type = DingDongCommentTypeNegative;
            break;
        default:
            break;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentView:refreshCommentWithType:)]) {
        [self.delegate commentView:self refreshCommentWithType:type];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"CommentCell";
    
    DDCommentTableCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    DDCommentObj *obj = self.commentAry[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.photoIcon sd_setImageWithURL:[NSURL URLWithString:obj.face] placeholderImage:nil];
    cell.nameLbl.text = obj.uname;
    if (obj.grade_moble.integerValue == 0) {
        if (obj.grade.integerValue == 1) {
            cell.starView.currentScore = 1;
        }else if (obj.grade.integerValue == 2){
            cell.starView.currentScore = 3;
        }else if (obj.grade.integerValue == 3){
            cell.starView.currentScore = 5;
        }else{
            cell.starView.currentScore = 5;
        }
    }else{
        cell.starView.currentScore = obj.grade_moble.integerValue;
    }
    cell.timeLbl.text = [ToolKit timeStampTurnDate:obj.dateline.stringValue withFormat:@"yyyy-MM-dd"];
    cell.createOrderTimeLbl.text = [NSString stringWithFormat:@"Buy date：%@",[ToolKit timeStampTurnDate:obj.order_create_time.stringValue withFormat:@"yyyy-MM-dd"]];
    cell.commentLbl.text = obj.content;
    if (obj.commentGalleryList.count == 0) {
        cell.imgBgViewHeight.constant = 0;
        cell.galleryView.hidden = YES;
    }else{
        cell.galleryView.hidden = NO;
        cell.galleryView.galleryAry = obj.commentGalleryList;
        cell.imgBgViewHeight.constant = 75;
    }
    return cell;
}
- (void)setCommentAry:(NSArray *)commentAry
{
    _commentAry = commentAry;
    [_reviewTable reloadData];
}
- (void)setLoadMore:(BOOL)loadMore
{
    _loadMore = loadMore;
    if (loadMore) {
        MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self footerRefresh];
        }];
        [self.reviewTable setMj_footer:footer];
    }
}
- (void)footerRefresh
{
    if (self.moreBlock) {
        self.moreBlock();
    }
}
- (void)footerWithRefreshingBlock:(void(^)())block
{
    self.moreBlock = block;
}
- (void)endRefreshing
{
    [self.reviewTable.mj_footer endRefreshing];
}
@end
